import Hls from 'hls.js'
export const getVideoPlay = (url, videoRef, isAutoPlay) => {
  const headers = {
    'Content-Type': 'application/x-mpegURL',
    'Access-Control-Allow-Origin': '*'
  }

  if (Hls.isSupported()) {
    const hls = new Hls({
      xhrSetup: xhr => {
        for (const header in headers) {
          xhr.setRequestHeader(header, headers[header])
        }
      }
    })
    hls.loadSource(url)
    hls.attachMedia(videoRef.value)
    if (isAutoPlay) {
      hls.on(Hls.Events.MANIFEST_PARSED, (_event, _data) => {
        videoRef.value.play()
      })
    }
  } else if (videoRef.value.canPlayType('application/vnd.apple.mpegurl')) {
    videoRef.value.src = url
    if (isAutoPlay) {
      videoRef.value.addEventListener('canplay', () => {
        videoRef.value.play()
      })
    }
  }
}
